package com.weiyirong99.crm.app.server;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import java.util.List;

import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

public class MyJPushMessageReceiver extends JPushMessageReceiver {

    public static String registrationID = "";

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        super.onMessage(context, customMessage);
        //消息到达 可指定类型，指定操作

//        NotificationBar.showNotifiBar(context, "我是消息", "我是消息内容");
        Log.d("WG", "onMessage: " + customMessage.message + "/" + customMessage.extra);
    }


    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageArrived(context, notificationMessage);
        String registrationID = JPushInterface.getRegistrationID(context.getApplicationContext());
        // 通知到达
        Log.d("WG", "onNotifyMessageArrived: " + notificationMessage.notificationExtras + " registrationID: " + registrationID);


    }


    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageOpened(context, notificationMessage);
        // 点击通知栏消息 可指定类型，指定操作
        Log.d("WG", "onNotifyMessageOpened: " + notificationMessage.notificationExtras);
        Log.e("WG", "onNotifyMessageOpened: 111" + isProcessExist(context, android.os.Process.myPid()));


    }


    @Override
    public void onRegister(Context context, String s) {
        super.onRegister(context, s);
        // 保存极光id
        Log.d("WG", "onRegister: id=" + s);


    }


    @Override
    public void onConnected(Context context, boolean b) {
        super.onConnected(context, b);
        Log.d("WG", "onConnected: " + b);
        if (b) {
            // 链接监听 保存极光id
            registrationID = JPushInterface.getRegistrationID(context.getApplicationContext());
            Log.d("WG", "onConnected: 连接成功获取id=" + registrationID);
        }

    }


    /**
     * 判断进程是否存活
     */
    public static boolean isProcessExist(Context context, int pid) {

        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> lists;
        if (am != null) {
            lists = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo appProcess : lists) {
                if (appProcess.pid == pid) {
                    Log.e("TAG", "333333" + pid);
                    return true;
                }
            }
        }
        return false;
    }

}
